<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AdminController
 *
 * @author Eugin
 */
class AdminController extends CustomControllerAction {

    public $configObj = null;

    public function preDispatch() {
        parent::preDispatch();

        $this->configObj = DatabaseObject_Config::GetConfigObject($this->db);
        
    }

    public function updateConfig($name, $value) {
        return DatabaseObject_Config::UpdateConfigValue($this->db, $name, $value);
    }

    public function indexAction() {
        $this->view->page_title = "Главная администратора";
        $stat = array();
        $stat['products'] = DatabaseObject_Products::GetCount($this->db);
        $stat['users'] = DatabaseObject_User::GetUsersCount($this->db);
        $stat['basket'] = DatabaseObject_Orders::GetCount($this->db);
        $stat['cartproduct'] = DatabaseObject_Orders::GetCountAllProduct($this->db);
        $stat['cartprice'] = CustomControllerAction::currency(DatabaseObject_Orders::GetSumPrice($this->db), '1', DatabaseObject_User::GetCurrencyUser($this->db, Zend_Auth::getInstance()->getIdentity()->id));
    
      //  CustomControllerAction::pre($stat);
        $this->view->stats = $stat;
   

    }

    public function categoriesAction() {
       echo  $cat =  $this->getRequest()->getParam('cat');
        $this->view->page_title = "Категории";
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['cat_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['cat_sort'])) {
            $temp = explode(' ', $_SESSION['cat_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'name';
            $order = 'asc';
            $_SESSION['cat_sort'] = $sort . ' ' . $order;
        }
        $page = max(1, $this->getRequest()->getQuery('p'));
        $limit = $this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
        $pages = ceil(DatabaseObject_Categories::GetCount($this->db) / $limit);
        if(!isset($cat)) $cat=0;
        $category = DatabaseObject_Categories::GetChildAdmin($this->db,$cat);
       // CustomControllerAction::pre($category);
                  $pag = CustomControllerAction::Pag($pages,$page);
        if ($cat != 0) {
            $kroshki = DatabaseObject_Categories::GetParentKroshki($this->db, $cat);
            $kroshki = array_reverse($kroshki);
            $this->view->kroshki = $kroshki;
        }
        $this->view->pages = $pag;
        $this->view->p = $page;
        $this->view->sort = $sort;
        $this->view->order = $order;
        $this->view->categories = $category;
    }

    public function addcategoryAction() {
        $this->view->page_title = "Добавление категории";
        $request = $this->getRequest();
        $fp = new FormProcessor_Category($this->db);

        $validate = $request->isXmlHttpRequest();

        if ($request->isPost()) {
            if ($validate) {
                $fp->validateOnly(true);
                $fp->process($request);
            } else if ($fp->process($request)) {
                $id = $fp->category->getId();
                $message = sprintf('Добавлена категория "%s" администратором "%s"', $fp->category->name, Zend_Auth::getInstance()->getIdentity()->login);

                $this->writeLog($message);

                // промах кэша
                $cats = DatabaseObject_Categories::GetAllParent($this->db);
                $this->cache->save($cats, 'leftcat');

                $this->_redirect($this->getCustomUrl(array(), 'addcategorycomplete'));
            }
        }

        if ($validate) {
            $json = array(
                'errors' => $fp->getErrors()
            );
            $this->sendJson($json);
        } else {
            $this->view->fp = $fp;
        }

        $this->view->fp = $fp;
        $this->view->cats = DatabaseObject_Categories::GetChild($this->db, 0);
    }

    public function editcategoryAction() {
        $this->view->page_title = "Редактирование категории";
        $id = $this->getRequest()->getParam('id');

        $fp = new FormProcessor_Category($this->db, $id);

        if ($this->getRequest()->isPost()) {
            if ($fp->process($this->getRequest())) {

                $message = sprintf('Категория "%s" была отредктирована администратором "%s"', $fp->category->name, Zend_Auth::getInstance()->getIdentity()->login);
                
                      // промах кэша
                $cats = DatabaseObject_Categories::GetAllParent($this->db);
                $this->cache->save($cats, 'leftcat');
                //  $this->writeLog($message);
                //  $this->_redirect($this->getCustomUrl(array(), 'editcategorycomplete'));
            }
        }
        $this->view->cats = DatabaseObject_Categories::GetChild($this->db, 0);
        $this->view->fp = $fp;
    }

    public function deletecategoryAction() {
        $id = $this->getRequest()->getParam('id');
        $category = new DatabaseObject_Categories($this->db);
        $category->load($id);
        $category->delete();
              // промах кэша
                $cats = DatabaseObject_Categories::GetAllParent($this->db);
                $this->cache->save($cats, 'leftcat');
        echo 'ok';
        exit;
    }

    public function addcategorycompleteAction() {
        // ничего не делаем, просто показываем шаблон
    }

    public function editcategorycompleteAction() {
        // ничего не делаем, просто показываем шаблон
    }

    public function usersAction() {
        $this->view->page_title = "Пользователи";
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['user_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['user_sort'])) {
            $temp = explode(' ', $_SESSION['user_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'id';
            $order = 'desc';
            $_SESSION['user_sort'] = $sort . ' ' . $order;
        }
        $page = max(1, $this->getRequest()->getQuery('p'));
        $type = $this->getRequest()->getQuery('type');
        $shop = $this->getRequest()->getQuery('shop');
        $limit = $this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
        $filter = array(
            'isApproved' => $type,
            'nameshop' => $shop
        );
        $pages = ceil(DatabaseObject_User::GetUsersCount($this->db, array(), $filter) / $limit);
        $users = DatabaseObject_User::GetUsers($this->db, array('order' => $_SESSION['user_sort'], 'limit' => $limit, 'offset' => $offset), $filter);
        $this->view->type = $type;
        $this->view->shop = $shop;
                 $pag = CustomControllerAction::Pag($pages,$page);
        $this->view->pages = $pag;
        $this->view->p = $page;
        $this->view->sort = $sort;
        $this->view->order = $order;
        $this->view->users = $users;
    }

    public function editusersAction() {
        $this->view->page_title = "Редактирование пользователя";
        $id = $this->getRequest()->getParam('id');
        $fp = new FormProcessor_EditProfile($this->db, $id);

        if ($this->getRequest()->isPost()) {
            if ($fp->process($this->getRequest())) {


                $message = sprintf('Изменение личных данных пользователя  %s админом ', $user->login);

                $this->writeLog($message);

                $this->_redirect($this->getUrl('users'));
            }
        }
        $temp = CustomControllerAction::getCountry();
        $this->view->countrylist = $temp['country'];
        $temp = CustomControllerAction::getRegion($fp->country_id);

        $this->view->regionlist = $temp['regions'];
        $temp = CustomControllerAction::getCity($fp->region_id);

        $this->view->citylist = $temp['citys'];
        $this->view->fp = $fp;
        $this->view->usermenu = 'edituser';
    }

    public function deleteuserAction() {
        $id = $this->getRequest()->getParam('id');
        $users = new DatabaseObject_User($this->db);
        $users->load($id);
        $users->delete();
        echo 'ok';
        exit;
    }

    public function approveuserAction() {
        $id = $this->getRequest()->getParam('id');
        $users = new DatabaseObject_User($this->db);
        $users->load($id);
        if ($users->IsApproved == '1')
            $users->isApproved = 0;
        else
            $users->isApproved = 1;
        echo 'ok';
        exit;
    }

    public function blockuserproductAction() {
        $id = $this->getRequest()->getParam('id');
        $users = new DatabaseObject_User($this->db);
        $users->load($id);
        if ($users->isCreateProduct == '1')
            $users->isCreateProduct = 0;
        else
            $users->isCreateProduct = 1;
        echo 'ok';
        exit;
    }

    public function productsAction() {
        $this->view->page_title = "Продукты";
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['products_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['products_sort'])) {
            $temp = explode(' ', $_SESSION['products_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'date_added';
            $order = 'DESC';
            $_SESSION['products_sort'] = $sort . ' ' . $order;
        }

        $page = max(1, $this->getRequest()->getQuery('p'));
        $cat = $this->getRequest()->getParam('cat');
        $type = $this->getRequest()->getParam('type');
        $limit =$this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
        $filter = array(
            'catId' => $cat,
            'type' => $type
        );
        $pages = ceil(DatabaseObject_Products::GetProductsCount($this->db, array('order' => $_SESSION['products_sort']), $filter) / $limit);
        $products = DatabaseObject_Products::GetProducts($this->db, array('order' => $_SESSION['products_sort'], 'limit' => $limit, 'offset' => $offset), $filter);
        $this->view->products = $products;
        $this->view->p = $page;
                $pag = CustomControllerAction::Pag($pages,$page);
        $this->view->pages = $pag;

        // if (!$cat) 
        $cats = DatabaseObject_Categories::GetAllParent($this->db);
        // else
        // $cats= DatabaseObject_Categories::GetParentId($this->db,$cat); 
        $kroshki = DatabaseObject_Categories::GetParentKroshki($this->db, $cat);
        $kroshki = array_reverse($kroshki);
        $this->view->kroshki = $kroshki;
        $this->view->categories = $cats;
        $this->view->cat = $cat;
        $this->view->type = $type;
        $this->view->sort = $sort;
        $this->view->order = $order;
        /*    $cats = DatabaseObject_Categories::GetCategories($this->db);
          $this->view->cats = $cats;
          $this->view->pages = $pages;
          $this->view->products = $products; */
    }

    public function editproductsAction() {
        $this->view->page_title = "Редактирование продукта";
        $id = $this->getRequest()->getParam('id');

        $fp = new FormProcessor_Products($this->db, $id);

        if ($this->getRequest()->isPost()) {
            if ($fp->process($this->getRequest())) {
                $message = sprintf('Изменена информация о продукте "%s" админом "%s"', $fp->products->name, Zend_Auth::getInstance()->getIdentity()->login);

                $this->writeLog($message);

                //$this->_redirect($this->getCustomUrl(array(), 'editproductscomplete'));
            }
        }

        $this->view->cats = DatabaseObject_Categories::GetCategories($this->db);

        $this->view->fp = $fp;
    }

    public function settingsAction() {
        $this->view->page_title = "Настройки";
        if ($this->getRequest()->isPost()) {
            $configs = array_keys($this->configuration);
            foreach ($configs as $key) {
                $this->updateConfig($key, $this->getRequest()->getPost($key));
            }

            $message = sprintf('Настройки сайта были изменены администратором "%s"', Zend_Auth::getInstance()->getIdentity()->login);

            $this->writeLog($message);

            $this->_redirect($this->getUrl('settings'));
        }
        $this->configObj1 = DatabaseObject_Config::GetConfigObject($this->db,'1');
        $this->configObj2 = DatabaseObject_Config::GetConfigObject($this->db,'2');
        $this->view->configObj = $this->configObj;
        $this->view->configObj1 = $this->configObj1;
        $this->view->configObj2 = $this->configObj2;
    }

    public function globalsettingsAction() {
        $this->view->page_title = "Глобальные настройки";
        if (Zend_Auth::getInstance()->getIdentity()->type != 'globaladmin') {
            $this->_redirect($this->getUrl('index'));
        }

        if ($this->getRequest()->isPost()) {
            $configItem = new DatabaseObject_Config($this->db);
            $configItem->config_name = $this->getRequest()->getPost('config_name');
            $configItem->config_display_name = $this->getRequest()->getPost('config_display_name');
            $configItem->config_value = '';
            $configItem->config_type = $this->getRequest()->getPost('config_type');
            $configItem->type = $this->getRequest()->getPost('type');
            $configItem->config_description = $this->getRequest()->getPost('config_description');
            $configItem->save();

            $this->configObj = DatabaseObject_Config::GetConfigObject($this->db);
        }
        $this->view->configObj = $this->configObj;
    }

    public function deleteconfigAction() {
        if (Zend_Auth::getInstance()->getIdentity()->type != 'globaladmin')
            $this->_redirect($this->getUrl('index'));

        $id = $this->getRequest()->getParam('id');

        $configItem = new DatabaseObject_Config($this->db);
        $configItem->load($id);
        $configItem->delete();

        $this->_redirect($this->getUrl('globalsettings'));
    }

    public function editconfigAction() {
        $this->view->page_title = "Редактирование настроек";
        if (Zend_Auth::getInstance()->getIdentity()->type != 'globaladmin')
            $this->_redirect($this->getUrl('index'));

        $id = $this->getRequest()->getParam('id');

        $configItem = new DatabaseObject_Config($this->db);
        $configItem->load($id);

        if ($this->getRequest()->isPost()) {
            $configItem->config_name = $this->getRequest()->getPost('config_name');
            $configItem->config_display_name = $this->getRequest()->getPost('config_display_name');
            $configItem->config_type = $this->getRequest()->getPost('config_type');
            $configItem->type = $this->getRequest()->getPost('type');
            $configItem->config_description = $this->getRequest()->getPost('config_description');
            $configItem->save();

            $this->_redirect($this->getUrl('globalsettings'));
        }

        $this->view->configItem = $configItem;
    }

    public function helpAction() {
        $catsRoot = DatabaseObject_HelpCategories::GetCategories($this->db);
        foreach ($catsRoot as $cat) {
            $cat->childs = DatabaseObject_HelpCategories::GetCategories($this->db, $cat->getId());
        }

        $this->view->cats = $catsRoot;
    }

    public function addhelpcatAction() {
        if ($this->getRequest()->isPost()) {

            $errors = array();

            $cat = new DatabaseObject_HelpCategories($this->db);
            $cat->alias = $this->getRequest()->getPost('alias');
            $cat->name = $this->getRequest()->getPost('name');
            $cat->parent_id = $this->getRequest()->getPost('parent');
            $cat->descr = $this->getRequest()->getPost('descr');

            if (strlen($cat->name) == 0)
                $errors['name'] = 'Вы должны указать название категории';

            if (strlen($cat->alias) == 0)
                $errors['alias'] = 'Вы должны указать ссылку для категории';
            if (empty($_FILES) && $cat->parent_id == 0)
                $errors['image'] = 'Вы должны загрузить изображение категории';

            if (empty($errors)) {
                $cat->image = 'tmp';
                $cat->save();

                // сохраним вложение
                $image = $_FILES['image'];
                $info = getimagesize($image['tmp_name']);
                $ext = $info[2];

                if ($ext == 2) {
                    $ext = 'jpg';
                } else if ($ext == 3) {
                    $ext = 'png';
                } else
                    $ext = 'gif';

                $new_name = $cat->getId() . '.' . $ext;

                move_uploaded_file($image['tmp_name'], Zend_Registry::get('config')->paths->base . '/www/uploads/helpcats/' . $new_name);

                $cat->image = $new_name;
                $cat->save();

                $this->_redirect($this->getUrl('help'));
            }
            else {
                $this->view->cat_name = $cat->name;
                $this->view->cat_alias = $cat->alias;
                $this->view->errors = $errors;
            }
        }

        $cats = DatabaseObject_HelpCategories::GetCategories($this->db);
        $this->view->cats = $cats;
    }

    public function addhelppageAction() {
        $id = $this->getRequest()->getParam('cat');

        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($id);

        $fp = new FormProcessor_AddHelpPage($this->db);

        if ($this->getRequest()->isPost()) {
            if ($fp->process($this->getRequest())) {
                $this->_redirect($this->getCustomUrl(array('id' => $cat->getId()), 'viewhelpcat'));
            }
        }

        $this->view->fp = $fp;

        $this->view->cat = $cat;
    }

    public function edithelppageAction() {
        $id = $this->getRequest()->getParam('id');

        $idc = $this->getRequest()->getParam('cat');

        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($idc);

        $fp = new FormProcessor_EditHelpPage($this->db, $id);

        if ($this->getRequest()->isPost()) {
            //$this->pre($this->getRequest()->getPost());return;
            if ($fp->process($this->getRequest())) {
                $this->_redirect($this->getCustomUrl(array('id' => $cat->getId()), 'viewhelpcat'));
            }
        }

        $this->view->fp = $fp;
    }

    public function deletehelppageAction() {
        $id = $this->getRequest()->getParam('id');

        $page = new DatabaseObject_HelpPages($this->db);
        $page->load($id);

        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($page->cat_id);

        $page->delete();

        $this->_redirect($this->getCustomUrl(array('id' => $cat->getId()), 'viewhelpcat'));
    }

    public function deletehelpcatAction() {
        $id = $this->getRequest()->getParam('id');

        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($id);
        $cat->delete();

        $this->_redirect($this->getUrl('help'));
    }

    public function edithelpcatAction() {
        $id = $this->getRequest()->getParam('id');
        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($id);

        if ($this->getRequest()->isPost()) {
            $cat->alias = $this->getRequest()->getPost('alias');
            $cat->name = $this->getRequest()->getPost('name');
            $cat->parent_id = $this->getRequest()->getPost('parent');
            $cat->descr = $this->getRequest()->getPost('descr');
            $cat->save();

            if (isset($_FILES['image']) && is_array($_FILES['image'])) {
                // сохраним вложение
                $image = $_FILES['image'];
                $info = getimagesize($image['tmp_name']);
                $ext = $info[2];

                if ($ext == 2) {
                    $ext = 'jpg';
                } else if ($ext == 3) {
                    $ext = 'png';
                } else
                    $ext = 'gif';

                $new_name = $cat->getId() . '.' . $ext;
                if ($cat->image) {
                    unlink(Zend_Registry::get('config')->paths->base . '/www/uploads/helpcats/' . $new_name);
                }

                move_uploaded_file($image['tmp_name'], Zend_Registry::get('config')->paths->base . '/www/uploads/helpcats/' . $new_name);

                $cat->image = $new_name;
                $cat->save();
            }

            $this->_redirect($this->getUrl('help'));
        }

        $cats = DatabaseObject_HelpCategories::GetCategories($this->db);
        $this->view->cats = $cats;
        $this->view->mtime = time();
        $this->view->cat = $cat;
    }

    public function viewhelpcatAction() {
        $id = $this->getRequest()->getParam('id');

        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($id);

        $pages = DatabaseObject_HelpPages::GetPages($this->db, $id);

        $this->view->cat = $cat;
        $this->view->pages = $pages;
    }

    public function basketAction() {
        $this->view->page_title = "Все покупки";
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['cart_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['cart_sort'])) {
            $temp = explode(' ', $_SESSION['cart_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'date';
            $order = 'desc';
            $_SESSION['cart_sort'] = $sort . ' ' . $order;
        }
        $page = max(1, $this->getRequest()->getQuery('p'));
        $type = $this->getRequest()->getQuery('type');
        $limit = $this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
        $pages = ceil(DatabaseObject_Orders::GetCount($this->db,$type) / $limit);
        $category = DatabaseObject_Orders::GetBasket($this->db, array('order' => $_SESSION['cart_sort'], 'where' => "status!='cart' and status!='favorite'", 'limit' => $limit, 'offset' => $offset),$type);
        // CustomControllerAction::pre($category);
               $pag = CustomControllerAction::Pag($pages,$page);
        $this->view->pages = $pag;
        $this->view->page = $page;
        $this->view->sort = $sort;
          $this->view->type = $type;
        $this->view->order = $order;
        $this->view->categories = $category;
    }

    public function deletezakazAction() {
        $id = $this->getRequest()->getParam('id');
        $users = new DatabaseObject_Basket($this->db);
        $users->load($id);
        $users->delete();
        echo 'ok';
        exit;
    }

    public function zakazAction() {
        $this->view->page_title = "Просмотр заказа";
        $id = $this->getRequest()->getParam('id');
        $zakaz = new DatabaseObject_Orders($this->db);
        $zakaz->load($id);
        // echo '<pre>';print_r($zakaz);echo '</pre>';
        $this->view->zakaz = $zakaz;
    }

    public function requestsAction() {
        $this->view->page_title = "Персональные заказы";
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['request_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['request_sort'])) {
            $temp = explode(' ', $_SESSION['request_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'id';
            $order = 'asc';
            $_SESSION['request_sort'] = $sort . ' ' . $order;
        }
        $page = max(1, $this->getRequest()->getQuery('p'));
        $limit = $this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
        $pages = ceil(DatabaseObject_Persorder::GetCount($this->db) / $limit);
        $category = DatabaseObject_Persorder::GetRequest($this->db, array('order' => $_SESSION['request_sort'], 'limit' => $limit, 'offset' => $offset));
        // CustomControllerAction::pre($category);
                 $pag = CustomControllerAction::Pag($pages,$page);
        $this->view->pages = $pag;
        $this->view->page = $page;
        $this->view->sort = $sort;
        $this->view->order = $order;
        $this->view->categories = $category;
    }

    public function deleterequestsAction() {
        $id = $this->getRequest()->getParam('id');
        $users = new DatabaseObject_Persorder($this->db);
        $users->load($id);
        $users->delete();
        echo 'ok';
        exit;
    }

    public function reviewsAction() {
        $this->view->page_title = "Комментарии";
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['reviews_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['reviews_sort'])) {
            $temp = explode(' ', $_SESSION['reviews_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'date';
            $order = 'asc';
            $_SESSION['reviews_sort'] = $sort . ' ' . $order;
        }
        $page = max(1, $this->getRequest()->getQuery('p'));
        $type =  $this->getRequest()->getQuery('type');
        $limit = $this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
        $pages = ceil(DatabaseObject_ShopReviews::GetCount($this->db,$type) / $limit);
        $category = DatabaseObject_ShopReviews::GetReviews($this->db, array('order' => $_SESSION['request_sort'], 'limit' => $limit, 'offset' => $offset),$type);
       
         $pag = CustomControllerAction::Pag($pages,$page);
          
        $this->view->pages = $pag;
        $this->view->page = $page;
         $this->view->type = $type;
        $this->view->sort = $sort;
        $this->view->order = $order;
        $this->view->categories = $category;
    }

    public function deletereviewsAction() {
        $id = $this->getRequest()->getParam('id');
        $users = new DatabaseObject_Persorder($this->db);
        $users->load($id);
        $users->delete();
        echo 'ok';
        exit;
    }
    
    ////// Тарифы ///
     public function addtarifAction() {
        $this->view->page_title = "Тарифы";
       
       
       
    }
    public function tarifAction() {
        $this->view->page_title = "Тарифы";
        if ($this->getRequest()->isPost()) {
            $tarif = new DatabaseObject_Tarif($this->db);
            $tarif->name = $this->getRequest()->getPost('name');
            $tarif->count_day = "1";
            $tarif->count_product = $this->getRequest()->getPost('count_product');
            $tarif->price = $this->getRequest()->getPost('price');
            $tarif->save();
        }
        $tarif = DatabaseObject_Tarif::GetTarifs($this->db);
        $this->view->tarif = $tarif;
    }
     public function deletetarifAction() {
        $id = $this->getRequest()->getParam('id');
        $tarif = new DatabaseObject_Tarif($this->db);
        $tarif->load($id);
        $tarif->delete();
        echo 'ok';
        exit;
    }
    
    public function edittarifAction() {
        $this->view->page_title = "Редактирование тарифа";
        

        $id = $this->getRequest()->getParam('id');

        $tarif = new DatabaseObject_Tarif($this->db);
        $tarif->load($id);

        if ($this->getRequest()->isPost()) {
            $tarif = new DatabaseObject_Tarif($this->db);
            $tarif->name = $this->getRequest()->getPost('name');
            $tarif->count_day = "1";
            $tarif->count_product = $this->getRequest()->getPost('count_product');
            $tarif->price = $this->getRequest()->getPost('price');
            $tarif->save();

            $this->_redirect($this->getUrl('tarif'));
        }

        $this->view->tarif = $tarif;
    }
///////////////////////////
}
